package com.lebo.portal.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lebo.portal.repository.BlogMapper;
import com.lebo.portal.view.PageQuery;
import com.lebo.portal.view.PageResult;
import com.lebo.web.domain.Blog;

@Service
public class BlogService extends BaseService {
    @Autowired
    private BlogMapper blogMapper;

    public PageResult<List<Blog>> pagingBlogs(PageQuery<Blog> pageQuery) {
        Integer categoryId = null != pageQuery.getFilter() ? pageQuery.getFilter().getCategoryId() : Integer.valueOf(1);
        List<Blog> blogs = blogMapper.getBlogs(categoryId, pageQuery.getPageInfo().getCurrPage(),
                pageQuery.getPageInfo().getPageSize());

        blogs.stream().forEach(b -> {
            b.getBlogs().stream().forEach(c -> {
                c.setCreateYear().setCreateMMdd();
            });
        });

        Long totalItem = blogMapper.getTotalItem(categoryId);
        return this.processResult(blogs, pageQuery, totalItem);
    }

    public Blog findSingleBlog(long id) throws Exception {
        return blogMapper.<Long>getDetailsBytId(id);
    }
}
